CMU 15-112 Summer 2019: Fundamentals of Programming and Computer Science
Homework 6 (Due Sun 2-Jun, at 5pm)




  1. Feedback Form [5 pts]
    Fill out this form to let us know how we are doing. This form is anonymous. Please take time to give us meaningful feedback. We want to make this class the best it can be for you! At the end, we will ask you to fill out another form so you get credit on hw6. We won't be able to match up the submissions.

  2. isKingsTour(board) [20 pts]
    Background: in Chess, a King can move from any square to any adjacent square in any of the 8 possible directions. A King's Tour is a series of legal King moves so that every square is visited exactly once. We can represent a Kings Tour in a 2d list where the numbers represent the order the squares are visited, going from 1 to N2. For example, consider these 2d lists:
       [ [  3, 2, 1 ],        [ [  1, 2, 3 ],      [ [ 3, 2, 1 ],
         [  6, 4, 9 ],          [  7, 4, 8 ],        [ 6, 4, 0 ],
         [  5, 7, 8 ] ]         [  6, 5, 9 ] ]       [ 5, 7, 8 ] ]
    
    The first is a legal Kings Tour but the second is not, because there is no way to legally move from the 7 to the 8, and the third is not, because it contains a 0 which is out of range. Also, this should work not just for 3x3 boards but for any NxN board. For example, here is a legal Kings Tour in a 4x4 board:
        [ [  1, 14, 15, 16],
          [ 13,  2,  7,  6],
          [ 12,  8,  3,  5],
          [ 11, 10,  9,  4]
        ]
    
    With this in mind, write the function isKingsTour(board) that takes a 2d list of integers, which you may assume is NxN for some N>0, and returns True if it represents a legal Kings Tour and False otherwise.

  3. drawStar(canvas, centerX, centerY, diameter, numPoints, color) [20 pts] [manually graded]
    Write the function drawStar which takes a canvas and the star's center coordinates, diameter, number of points, and color, and produces a star based on that specification. To draw a star, we need to identify where to place each of the inner and outer points, then draw them all together as a polygon.

    The outer points of the star should be evenly placed on a circle based on the specified diameter, with the first point at a 90 degree angle. The inner points should then be placed on a circle 3/8 the size of the first circle, halfway between the pairs of outer points. (We use this ratio to make a nice-looking five-pointed star. Actually, the best inner circle would be about 38.2% the size of the outer circle; a little trigonometry and problem-solving will tell you why! But 3/8 is close enough.) An example of how these circles work is shown below.




    For example, this call:
       drawStar(canvas, 250, 250, 500, 5, "gold")
    produces this result:

    And this call:
       drawStar(canvas, 300, 400, 100, 4, "blue")
    produces this result:

    And if we add a few more points:
       drawStar(canvas, 300, 200, 300, 9, "red")
    we get this result:



  4. drawSudokuBoard(canvas, board, margin, canvasSize) [55 pts] [manually graded]
    Write the function drawSudokuBoard that draws a sudoku board as follows:


    Here are some details on the parameters:
    • board is a NxN list of integers. You may assume that it contains only integers.
    • margin is the distance between the grid and the edge of the screen.
    • canvasSize is equal to both the width and height of the canvas.

    You must satisfy the following additional requirements to get full credit:
    • Your code should be able to handle any board size that is rectangular and has at least 4 rows. That is, we can give you a board that is 4x4, 9x9, etc. The size of the board should adjust to fit the whole board on the screen.
    • You should be able to handle any canvasSize. The size of the board should adjust to fit the whole board on the screen.
    • Make sure to have all of the details in the picture: the thicker lines, the numbers, etc.
    • Your margin must be exactly what we specified. That is: your board must expand to fill up the full screen leaving only the margin given as whitespace.
    • Choose a reasonable font size that works with most boards. You don't have to adjust that font size when the canvasSize/margin/board dimensions change.

    Here's an example of a board with a large margin:


    Here's an example of a board that is 4x4:


    Here's an example of a board that has a small canvasSize: